//
//  ReceiveAwardController.swift
//  SCA
//
//  Created by tjun on 2025/9/13.
//

import UIKit
import SnapKit

class ReceiveAwardController: BaseController {
    
    private let titleLabel = UILabel(text: "可领取奖励".localizedString, font: UIFontMediumMake(14), textColor: .white)
    private let valueLabel = UILabel(text: "$0.0000", font: UIFontSemiboldMake(30), textColor: .white)
    private let confirmButton = MainButton(type: .system, title: "立即领取".localizedString, font: UIFontMake(16), textColor: .white)
    
    private let headerView = UIView()

    private var datas: [ReceiveAwardModel] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 40
        tableView.backgroundColor = UIColorMake(0x2E323F)
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(ReceiveAwardCell.self, forCellReuseIdentifier: ReceiveAwardCell.identifier)
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.separatorStyle = .none
        tableView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        headerView.addTo(view)
        headerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navBarHeight)
        }
        
        titleLabel.addTo(headerView)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        valueLabel.addTo(headerView)
        valueLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.bottom.equalTo(-20)
        }
        
        tableView.addCorner(radius: 10)
        tableView.addTo(self.view)
        tableView.isScrollEnabled = false
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(headerView.snp.bottom)
            make.height.equalTo(0)
        }
        
        let footerView = UIView()
        footerView.addTo(self.view)
        footerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(tableView.snp.bottom)
        }
        
        let hintLabel = UILabel(text: "领取成功后，收益将自动计入您的资产账户，其中1%注入熔炉基金进行销毁".localizedString, font: UIFontMake(12), textColor:UIColorMake(0x959595), textAlignment: .left).addTo(footerView)
        hintLabel.attributedText = hintLabel.text?.titleMargin(3, font: UIFontMake(12), alignment: .left)
        hintLabel.numberOfLines = 0
        hintLabel.addTo(footerView)
        hintLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(tableView.snp.bottom).offset(22)
            make.bottom.equalToSuperview().offset(-10)
        }

        confirmButton.addTo(view)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.bottom.equalTo(-tabBarSafeBottomMargin - 20)
        }
        
        loadData()
    }
      
    private func loadData() {
        
        ComputingPowerService.loadReceiveAwardData(success:{ info in
            self.valueLabel.text = info.total_amount_disp
            self.confirmButton.isEnabled = info.claimable
            self.datas.removeAll()
            self.datas.append(contentsOf: info.models)
            
            let height = self.tableView.contentInset.top + self.tableView.contentInset.bottom + CGFloat(info.models.count) * 40
            
            self.tableView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview().inset(AppStyle.margin.left)
                make.top.equalTo(self.headerView.snp.bottom)
                make.height.equalTo(height)
            }
            self.tableView.reloadData()
        })
    }
    
    @objc func confirmButtonTapped() {
        HUD.showLoadingInView(self.view)
        ComputingPowerService.receiveAward {
            HUD.showMessage("领取成功".localizedString)
            self.loadData()
        } failure: { error in
            HUD.showErrorMessage(error)
        }
    }
    
    
}

extension ReceiveAwardController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ReceiveAwardCell.identifier, for: indexPath) as! ReceiveAwardCell
        cell.model = datas[indexPath.row]
        return cell
    }
}


